<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Gate;
VERIFYNAMESPACE

use App\Models\CLASSNAME;


class CLASSNAMEController extends ResourceController
{
VERIFYCONTENT
   /*
    |--------------------------------------------------------------------------
    |  CLASSNAME Controller
    |--------------------------------------------------------------------------
    |
    | CLASSNAME控制器，CLASSNAME的CURD
    |
    */

    /**
     * beforeUpdate
     * 组合需要更新的数组
     *
     * @return \Illuminate\Http\Response
     */
    public function beforeUpdate($datas,$detail)
    {
        $returnData['status'] = true;
        $returnData['msg'] = 'success';

        //是否禁用
        $datas['active'] = isset($datas['active'])?1:0;

       //判断是否修改图片
       if(isset($datas['imgpath'])) {
            if($datas['imgpath'] != $detail['imgpath']&&(Storage::exists($datas['imgpath']))){
                //移动图片 并更新
                Storage::move($datas['imgpath'], str_replace('temp','common',$datas['imgpath']));
                $datas['imgpath']=str_replace('temp','common',$datas['imgpath']);
            } else {
                $datas['imgpath']=$detail['imgpath'];
            }
       }
       

        $returnData['data'] = $datas;
        return $returnData;
    }

     /**
     * beforeStore
     * 组合需要更新的数组
     *
     * @return \Illuminate\Http\Response
     */
    public function beforeStore($datas)
    {
        $returnData['status'] = true;
        $returnData['msg'] = 'success';

        //是否禁用
        $datas['active'] = isset($datas['active'])?1:0;


       //判断是否修改图片
       if(isset($datas['imgpath'])) {
            if(Storage::exists($datas['imgpath'])){
                //移动图片 并更新
                Storage::move($datas['imgpath'], str_replace('temp','common',$datas['imgpath']));
                $datas['imgpath']=str_replace('temp','common',$datas['imgpath']);
            } else {
                $datas['imgpath']='';
            }
       }
       

        $returnData['data'] = $datas;
        return $returnData;
    }

    /**
     * afterUpdate
     * 后置操作，如果修改了图片，删除之前的
     *
     * @return \Illuminate\Http\Response
     */
    public function afterUpdate($returnData,$detail)
    {
        //删除替换之前的图片
        $request = $this->getFormRequest();
        if(isset($detail['imgpath'])) {
            $imgpath = $request->input('imgpath','');
            if($returnData['status']&&$imgpath != $detail['imgpath']&&Storage::exists($detail['imgpath'])){
                Storage::delete($detail['imgpath']);
            }
        }
        
        return $returnData;
    }

    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function afterDelete($result,$detail)
    {
        //
        if($result&&isset($detail['imgpath'])&&Storage::exists($detail['imgpath'])){
            Storage::delete($detail['imgpath']);
        }
        return true;
    }

    /**
     * formatData
     *
     * @return \Illuminate\Http\Response
     */
    public function formatData($data)
    {
        //
        foreach ($data as $key => $value) {
            $data[$key]['created_date'] = date('Y-m-d',$value['created_at']?$value['created_at']->timestamp:0);
            $data[$key]['updated_date'] = date('Y-m-d',$value['updated_at']?$value['updated_at']->timestamp:0);
        }
        return $data;
    }
}
